package com.sz.sgs.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sz.sgs.common.HttpClientUtils;
import com.sz.sgs.common.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FanUserService {

    @Autowired
    private TokenUtils tokenUtils;

    //获取所有fans列表信息；
    public String getAllFans(){
        String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+tokenUtils.getToken()+"&next_openid=";
        String result = HttpClientUtils.getMethod(url);
        //System.out.println(result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data  = (JSONObject) jsonObject.get("data");
        List<String> list = JSON.parseArray(data.get("openid").toString(),String.class);
        for (String s : list) {
            getDetails(s);    //循环获取用户的详细信息，并打印输出来：
            //http://thirdwx.qlogo.cn/mmopen/Q3auHgzwzM4lcbv5Cak2QfhGbibkFHKQicLTeAjfsHib3TCN7Q0ZeBtAxS2QLv37bJibMqUtxpaLzagXPVGWZxKJtnqdR1X2ptqOibNa5o4eVrXo/132

            //群发消息；
            System.out.println(sendMsgToFans(s));
        }
        return result;
    }

    //给一个粉丝用户发送一条文本消息；
    private String sendMsgToFans(String s) {
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+tokenUtils.getToken();
        String json = "{\n" +
                "    \"touser\":\""+s+"\",\n" +
                "    \"msgtype\":\"text\",\n" +         //text : 表示发送的是文本信息； 不能改！
                "    \"text\":\n" +
                "    {\n" +
                "         \"content\":\"深圳知领预祝Java44班全部高薪就业！！！SpringBoot集成微信接口调用项目正式启动了！晚上的作业是：把所有关注的粉丝用户详细信息显示在页面上，并插入到mysql数据库中！\"\n" +
                "    }\n" +
                "}";
        String result = HttpClientUtils.postMethod(url,json);
        return result;
    }

    //自定义菜单；
    public String createMenus(){
        String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+tokenUtils.getToken();
        String json = "{\n" +
                "    \"button\": [\n" +
                "        {\n" +
                "            \"name\": \"扫码\",\n" +
                "            \"sub_button\": [\n" +
                "                {\n" +
                "                    \"key\": \"rselfmenu_0_0\",\n" +
                "                    \"name\": \"扫码带提示\",\n" +
                "                    \"sub_button\": [],\n" +
                "                    \"type\": \"scancode_waitmsg\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"key\": \"rselfmenu_0_1\",\n" +
                "                    \"name\": \"扫码推事件\",\n" +
                "                    \"sub_button\": [],\n" +
                "                    \"type\": \"scancode_push\"\n" +
                "                }\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"发图\",\n" +
                "            \"sub_button\": [\n" +
                "                {\n" +
                "                    \"key\": \"rselfmenu_1_0\",\n" +
                "                    \"name\": \"系统拍照发图\",\n" +
                "                    \"sub_button\": [],\n" +
                "                    \"type\": \"pic_sysphoto\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"key\": \"rselfmenu_1_1\",\n" +
                "                    \"name\": \"拍照或者相册发图\",\n" +
                "                    \"sub_button\": [],\n" +
                "                    \"type\": \"pic_photo_or_album\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"key\": \"rselfmenu_1_2\",\n" +
                "                    \"name\": \"微信相册发图\",\n" +
                "                    \"sub_button\": [],\n" +
                "                    \"type\": \"pic_weixin\"\n" +
                "                }\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "           \"name\":\"玩游戏\",\n" +
                "           \"sub_button\":[\n" +
                "           {\t\n" +
                "               \"type\":\"click\",\n" +
                "               \"name\":\"石头\",\n" +
                "              \"key\":\"V1001_GOOD3\"\n" +
                "            },\n" +
                "            {\n" +
                "                 \"type\":\"click\",\n" +
                "                 \"name\":\"剪刀\",\n" +
                "            \t\"key\":\"V1001_GOOD2\"\n" +
                "             },\n" +
                "            {\n" +
                "               \"type\":\"click\",\n" +
                "               \"name\":\"布\",\n" +
                "               \"key\":\"V1001_GOOD\"\n" +
                "            }]\n" +
                "       }\n" +
                "    ]\n" +
                "}";
        String result = HttpClientUtils.postMethod(url, json);
        return result;
    }


    //获取用户的详细信息；
    public String getDetails(String openid){
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+tokenUtils.getToken()+"&openid="+openid+"&lang=zh_CN";
        String result = HttpClientUtils.getMethod(url);
        System.out.println("获取用户的详细信息result = " + result);
        return result;
    }

}
